import msg from "../../../../utils/msg";
import { url } from "../../../../utils/request/route";
import { leaveHistory } from "../../../../utils/request/api";
import User from "../../../../utils/user";

// pages/user/leave/leaveHistory/leaveHistory.ts
export interface leaveHisroty{
  schoolYear:string,
  semester:number,
  reason:string,
  create_time:string,
  status:string,
  judgeNote:string,
  judge_user:string,
  judge_time:string,
  sr_name:string,
  start:string,//开始日期
  end:string,//结束日期
  start_time:string,//第几节课开始
  end_time:string,//第几节课结束
  id:number,
}
var user = new User;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url:url,
    showData:[] as leaveHisroty[],
    originDataInvalid:[] as leaveHisroty[],//已过期数据
    originDataValid:[] as leaveHisroty[],//未过期数据
    index:0 as number,//展示详情所用
    tabIndex:0 as number,//切换tab所用
    detailShow:false,
    input:'' as string,
  },
  //切换tab
  onChange(e:any){
    let index = e.detail.index;
    let showData=[];
    if(index==0){
      showData=this.data.originDataValid;
    }
    else{
      showData=this.data.originDataInvalid;
    }
    this.setData({
      showData:showData,
      tabIndex:index,
    })
    if(this.data.input!=''){
      this.search(this.data.input);
    }

  },
  onClickLeft(){
    wx.navigateBack();
  },
  showDetail(e:any){
    let index = e.currentTarget.dataset.index;
    this.setData({
      index:index,
      detailShow:true,
    })
  },
  //获取输入
  getInput(e:any){
    let input = e.detail.value;
    this.setData({
      input:input,
    });
    this.search(input);
  },
  search(input:string){
    let data = this.data.showData;
    let showData=[] as leaveHisroty[];
    data.forEach((item,index)=>{
      if(item.status==input || item.reason.includes(input)){
        showData.push(item);
      }
    });
    if(input=="") {
      if(this.data.tabIndex==0)
        showData=this.data.originDataValid;
      else showData=this.data.originDataInvalid;
    }
    this.setData({showData:showData});
  },
  //分出未过期与已过期测数据
  dataSeparate(originData:leaveHisroty[]){
    let originDataInvalid = [] as leaveHisroty[];
    let originDataValid = [] as leaveHisroty[];
    originData.forEach((item,index)=>{
      if(this._isDatePassed(item.end)){
        originDataInvalid.push(item);
      }
      else originDataValid.push(item);
    });
    this.setData({
      originDataValid:originDataValid,
      originDataInvalid:originDataInvalid,
    });
  },
  _isDatePassed(inputDate:string) {
    var currentDate = new Date();
    var inputDateArr = inputDate.split('/');
    var year = parseInt(inputDateArr[0]);
    var month = parseInt(inputDateArr[1]) - 1; // 月份从0开始计数，需要减1
    var day = parseInt(inputDateArr[2]);
    console.log(inputDate);
    var targetDate = new Date(year, month, day);
    // 将输入日期和当前日期转换为时间戳进行比较
    if (targetDate.getTime()+ 86400000 <= currentDate.getTime()) {
      return true;
    } else {
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  onClose(){
    this.setData({
      detailShow:false,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    leaveHistory({s_code:user.code})
    .then((res:any)=>{
      this.dataSeparate(res.data.data);
      this.setData({
        showData:this.data.originDataValid,
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})